package com.jingdianjichi.subject.service.impl;


import com.alibaba.fastjson.JSON;

import com.jingdianjichi.subject.converter.EmployeeConverter;
import com.jingdianjichi.subject.eneity.EmployeeBO;
import com.jingdianjichi.subject.infra.basic.entity.Employee;
import com.jingdianjichi.subject.infra.basic.service.EmployeeService;
import com.jingdianjichi.subject.service.EmployeeDomainService;
import com.jingdianjichi.subject.util.RestIndexClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


import javax.annotation.Resource;

@Service
@Slf4j
public class EmployeeDomainServiceImpl implements EmployeeDomainService {

    @Resource
    private EmployeeService employeeService;



    public void delEmployee(EmployeeBO employeeBO) {
        Employee employee = EmployeeConverter.INSTANCE.converterBoToEmployee(employeeBO);
        employeeService.deleteById(employee.getId());
    }

    public void updateEmployee(EmployeeBO employeeBO) {
        Employee employee = EmployeeConverter.INSTANCE.converterBoToEmployee(employeeBO);
        employeeService.update(employee);
    }

    public void addEmployee(EmployeeBO employeeBO) {
        if (log.isInfoEnabled()) {
            log.info("EmployeeController.add.bo:{} ", JSON.toJSONString(employeeBO));
        }
        Employee employee = EmployeeConverter.INSTANCE.converterBoToEmployee(employeeBO);
        employeeService.insert(employee);
    }

    public void deleteByIds(List<Integer> ids) {
        employeeService.deleteByIds(ids);

    }

    public List<EmployeeBO> selectPage(Integer pageNum, Integer pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<Employee> employees = employeeService.selectPage(offset, pageSize);
        List<EmployeeBO> employeeBOS = EmployeeConverter.INSTANCE.converterEmployeesToBos(employees);
        return employeeBOS;
    }


    public List<EmployeeBO> queryAllElement(EmployeeBO employeeBO) {
        Employee employee = EmployeeConverter.INSTANCE.converterBoToEmployee(employeeBO);
        List<Employee> employees = employeeService.queryAllElement(employee);
        List<EmployeeBO> employeeBOS = EmployeeConverter.INSTANCE.converterEmployeesToBos(employees);
        return employeeBOS;


    }

    public EmployeeBO queryById(String id) {
        Employee employee = employeeService.queryById(Integer.valueOf(id));
        EmployeeBO employeeBO = EmployeeConverter.INSTANCE.converterEmployeeToBo(employee);
        if (log.isInfoEnabled()) {
            log.info("EmployeeController.queryById.bo:{} ", JSON.toJSONString(employee));
        }
        return employeeBO;
    }


    //查询全部
    public List<EmployeeBO> queryAll() {
        List<Employee> employees = employeeService.queryAll();
        List<EmployeeBO> converterEmployeesToBos = EmployeeConverter.INSTANCE.converterEmployeesToBos(employees);
        ;
        if (log.isInfoEnabled()) {
            log.info("EmployeeController.queryAll.bo:{} ", JSON.toJSONString(converterEmployeesToBos));
        }
        return converterEmployeesToBos;
    }

    public String getNumber() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String format = simpleDateFormat.format(new Date());
        String maxNumber = employeeService.getCurrentNum(format);
        String employeeNumber;
        if (maxNumber == null){
             employeeNumber = format + "01";
        }else{
            String substring = maxNumber.substring(maxNumber.length() - 2);

            if (Integer.valueOf(substring) < 10){
                employeeNumber = format + "0" + String.valueOf(Integer.valueOf(substring) + 1);
            }else{
                employeeNumber = format + String.valueOf(Integer.valueOf(substring) + 1);
            }
        }
        return employeeNumber;



    }



}
